package InnerGameParts;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;


public class RemoteListServer<T extends Serializable> extends UnicastRemoteObject implements RemoteList<T> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3478334839649449699L;
	/**
	 * default serial id
	 */

	
	private List<T> myList;
	
	public RemoteListServer(final String name) throws RemoteException, MalformedURLException {
		super();
		myList = new LinkedList<T>();
		Naming.rebind(name, this);
		
	}

	@Override
	public synchronized  void  add(T arg0) throws RemoteException {
		
		myList.add(arg0);
		
	}

	public synchronized  void addAll(Collection<? extends T>  arg0){
	
		myList.addAll(arg0);
		
	}
	
	@Override
	public synchronized  void clear() throws RemoteException {
		
		myList.clear();
	}

	@Override
	public synchronized  boolean contains(Object arg0) throws RemoteException {

		return 		myList.contains(arg0);
	}
	
	public synchronized T getFirst()  throws RemoteException{
		
		if(  !myList.isEmpty()){
			
			return myList.get(0);
		}
		
		return null;
		
	}
	
	@Override
	public synchronized  T get(int arg0) throws RemoteException {
	
				
		
		return myList.get(arg0);
		
	}

	/*@Override
	public synchronized  int indexOf(Object arg0) throws RemoteException {
		
		return myList.indexOf(arg0);
		
	}*/

	@Override
	public synchronized boolean isEmpty() throws RemoteException {
		
		return myList.isEmpty();
		
	}

	@Override
	public synchronized  boolean remove(T arg0) throws RemoteException {
		
		return myList.remove(arg0);
		
	}

	@Override
	public synchronized  T remove(int arg0) throws RemoteException {
		
		return myList.remove(arg0);
		
	}

	@Override
	public synchronized  int size() throws RemoteException {
		
		return myList.size();
		
	}

	@Override
	public synchronized  void add(Collection<? extends T> arg0) throws RemoteException {

		myList.addAll(arg0);
		
	}

	
	public synchronized T next( T arg0)  throws RemoteException{

		int count = myList.indexOf(arg0);
		T nextObject = null;
		try{
		
			nextObject =  myList.get(count + 1);
			
		}catch (Exception e){
			
			return null;
			
		}
		
		return nextObject;
		
	}


}
